﻿using System;
using System.Windows.Forms;
using EasierRdp.Models;
using EasierRdp.Util;
using System.Threading;

namespace EasierRdp.Forms
{
    partial class ControlBar : Form
    {
        private readonly EasierRdp _mainForm ;

        public ControlBar(EasierRdp mainFrom)
        {
            InitializeComponent();
            _mainForm = mainFrom;
        }

        public void Rebind()
        {
            ComboServer.Items.Clear();
            foreach (var rdpSession in RdpContext.Sessions)
            {
                ComboServer.Items.Add(rdpSession.Value);
            }
            ComboSettings.Items.Clear();
            foreach (var rdpSetting in RdpContext.Settings)
            {
                ComboSettings.Items.Add(rdpSetting.Value);
            }
        }

        public void SetActiveSession(RdpSession session)
        {
            if (session != null)
            {
                foreach (var item in ComboServer.Items)
                {
                    var s = item as RdpSession;
                    if (s == session)
                    {
                        ComboServer.SelectedItem = item;
                    }
                }
                foreach (var item in ComboSettings.Items)
                {
                    var s = item as RdpSetting;
                    if (s == session.Setting)
                    {
                        ComboSettings.SelectedItem = item;
                    }
                }
            }
        }

        private void ButtonClose_Click(object sender, EventArgs e)
        {
            if (null != RdpContext.FrontSession)
            {
                RdpContext.FrontSession.Disconnect();
                _mainForm.ToolStripFullScreen_Click(sender, e);
            }
        }

        private void ButtonMax_Click(object sender, EventArgs e)
        {
            _mainForm.ToolStripFullScreen_Click(sender, e);
        }

        private void ButtonMin_Click(object sender, EventArgs e)
        {
            _mainForm.WindowState = FormWindowState.Minimized;
        }

        private void ComboServer_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = ComboServer.SelectedItem as RdpSession;
            if (item != null)
            {
                item.Show();
            }
        }

        private void ButtonReconnect_Click(object sender, EventArgs e)
        {
            if (RdpContext.FrontSession != null)
            {
                if (RdpContext.FrontSession.Connected)
                {
                    if (DialogResult.OK ==
                        MessageBox.Show("Do you want to disconnect and reconnect the session?", "Reconnect",
                                        MessageBoxButtons.OKCancel))
                    {
                        RdpContext.FrontSession.Disconnect();
                        Thread.Sleep(300);
                    }
                    RdpContext.FrontSession.Connect();
                }
            }
        }

        private void ButtonUnpin_Click(object sender, EventArgs e)
        {

        }

        private void ButtonPin_Click(object sender, EventArgs e)
        {

        }

        private void ComboSettings_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = ComboServer.SelectedItem as RdpSession;
            if (item != null)
            {
                var setting = ComboSettings.SelectedItem as RdpSetting;
                if (setting != null)
                {
                    item.ApplySetting(setting);
                }
            }
        }
    }
}
